package cn.tedu.ivos2.user.service.impl;

import cn.tedu.ivos2.base.exception.ServiceException;
import cn.tedu.ivos2.base.response.StatusCode;
import cn.tedu.ivos2.user.mapper.UserMapper;
import cn.tedu.ivos2.user.pojo.dto.UserLoginParam;
import cn.tedu.ivos2.user.pojo.vo.UserVO;
import cn.tedu.ivos2.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    public UserVO login(UserLoginParam param) {
        log.debug("开始处理用户登录业务："+param);
        UserVO userVO=userMapper.selectByUsername(param.getUsername());
        //一旦抛出异常，就会被全局异常处理器拦截到，并由全局异常处理器给前端响应结果
        if (userVO == null){
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if (!userVO.getPassword().equals(param.getPassword())){
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        //只有遇不到上面的两个异常，才会走到下面，由controller层给前端返回结果
        log.debug("查询结果："+userVO);
        return userVO;
    }

}
